import request from '@/utils/request'

const api = {
  user: '/user/',
  role: '/menu/list_role/',
  navRole: '/menu/nav_role/',
  service: '/service',
  permission: '/permission',
  permissionNoPager: '/permission/no-pager',
  orgTree: '/org/tree',
  menu: '/menu/nav/',
  action: '/menu/action'
}

export default api

export function getUserList (parameter) {
  return request({
    url: api.user,
    method: 'get',
    params: parameter
  })
}

export function addUser (parameter) {
  return request({
    url: api.user,
    method: 'post',
    data: parameter
  })
}

export function removeUser (parameter) {
  return request({
    url: `${api.user}${parameter.id}`,
    method: 'delete',
    data: parameter
  })
}

export function getRoleList (parameter) {
  return request({
    url: api.role,
    method: 'get',
    params: parameter
  })
}

export function addRole (parameter) {
  return request({
    url: api.role,
    method: 'post',
    data: parameter
  })
}

export function updateRole (parameter) {
  return request({
    url: `${api.role}${parameter.id}/`,
    method: 'patch',
    data: parameter
  })
}

export function removeRole (parameter) {
  return request({
    url: `${api.role}${parameter.id}/`,
    method: 'delete'
  })
}

export function getNavRoleList (parameter) {
  return request({
    url: api.navRole,
    method: 'get',
    params: parameter
  })
}

export function addNavRole (parameter) {
  return request({
    url: api.navRole,
    method: 'post',
    data: parameter
  })
}

export function updateNavRole (parameter) {
  return request({
    url: `${api.navRole}${parameter.id}/`,
    method: 'patch',
    data: parameter
  })
}

export function getMenuList (parameter) {
  return request({
    url: api.menu,
    method: 'get',
    params: parameter
  })
}

export function addMenu (parameter) {
  return request({
    url: api.menu,
    method: 'post',
    data: parameter
  })
}

export function updateMenu (parameter) {
  return request({
    url: `${api.menu}${parameter.id}/`,
    method: 'patch',
    data: parameter
  })
}

export function deleteMenu (id) {
  return request({
    url: `${api.menu}${id}/`,
    method: 'delete'
  })
}

export function getActionList (parameter) {
  return request({
    url: api.action,
    method: 'get',
    params: parameter
  })
}

export function getServiceList (parameter) {
  return request({
    url: api.service,
    method: 'get',
    params: parameter
  })
}

export function getPermissions (parameter) {
  return request({
    url: api.permissionNoPager,
    method: 'get',
    params: parameter
  })
}

export function getOrgTree (parameter) {
  return request({
    url: api.orgTree,
    method: 'get',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService (parameter) {
  return request({
    url: api.service,
    method: parameter.id === 0 ? 'post' : 'put',
    data: parameter
  })
}

export function saveSub (sub) {
  return request({
    url: '/sub',
    method: sub.id === 0 ? 'post' : 'put',
    data: sub
  })
}
